<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChCorp;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;
use Mine\MineModel;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ChCorpMapper extends AbstractMapper
{
    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChCorp::class;
    }

    #[Cacheable(prefix: "ch_corp_info",value: "id_#{corpId}",ttl: 15 * 60)]
    public function findById(mixed $corpId): ChCorp|Model|string|Builder|null
    {
        return $this->model::query()->where('id',$corpId)->first();
    }

    /**
     * 获取企业信息
     * @param mixed $corpId
     * @return MineModel|Model|string|Builder|null
     */
    #[Cacheable(prefix: "ch_corp_info",value: "corp_id_#{corpId}_#{mainUid}",ttl: 15 * 60)]
    public function findByCorpId(mixed $corpId,int $mainUid): ChCorp|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('main_uid',$mainUid)
            ->where('corp_id',$corpId)
            ->first();
    }

    #[Cacheable(prefix: "ch_auth_corp_info",value: "#{corpId}",ttl: 15 * 60)]
    public function findByAuthCorpId(string $corpId): ChCorp|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('auth_corp_id',$corpId)
            ->where('auth_status',1)
            ->first();
    }

    public function findByCorpName(mixed $corpName,int $mainUid): ChCorp|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('main_uid',$mainUid)
            ->where('corp_name',$corpName)
            ->first();
    }

    /**
     * 删除缓存
     * @param mixed $id
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function clearCache(mixed $id): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'ch_corp_info:id_'.$id;
        $redis->del([$keyOne]);
    }

    public function clearAuthCache(string $corpId): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'ch_auth_corp_info:'.$corpId;
        $redis->del([$keyOne]);
    }

    /**
     * 删除缓存
     * @param mixed $id
     * @return void
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function clearCacheByCorpId(mixed $corpId,int $mainUid): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'ch_corp_info:corp_id_'.$corpId.'_'.$mainUid;
        $redis->del([$keyOne]);
    }

    public function handleSearch(Builder $query, array $params): Builder
    {

        $query->where('main_uid',$params['main_uid']);

        $query->where('type',0);

        return $query;
    }
}